/**
 * 
 */
package com.xsoft.framework.service;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xsoft.framework.common.IbatisGenericDao;
import com.xsoft.framework.po.Persistent;
import com.xsoft.framework.util.Page;


/**
 * @author 潘冬 2009-7-13 下午02:52:45
 */
public abstract class BaseService {
	protected IbatisGenericDao dao;
	protected Log logger = LogFactory.getLog(getClass());
	protected String loadSQL = "load" + getTableName();
	protected String insertSQL = "insert" + getTableName();
	protected String updateSQL = "update" + getTableName();
	protected String deleteSQL = "delete" + getTableName();
	protected String defaultSelectSQL = "select" + getTableName();
	
	
	public void setDao(IbatisGenericDao dao) {
		this.dao = dao;
	}

	public IbatisGenericDao detDao() {
		return this.dao;
	}
	
	public void init(){
//		initModuleInfo();
	}
	
	public abstract String getTableName();
	
//	protected HashMap<Integer,String> initModuleFunction(){
//		HashMap<Integer,String> map = new HashMap<Integer,String>();
//		map.put(this.MODULE_OPERATING_SELECT, "查询");
//		map.put(this.MODULE_OPERATING_INSERT, "添加");
//		map.put(this.MODULE_OPERATING_UPDATE, "修改");
//		map.put(this.MODULE_OPERATING_DELETE, "删除");
//		return map;
//	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.aresoft.logic.service.interfaces.BaseLogicService#delete(java.lang
	 * .Long)
	 */
	public boolean delete(Long id) {
		// TODO Auto-generated method stub
//		operating = this.MODULE_OPERATING_DELETE;
		return dao.update(deleteSQL, id) > 0 ? true : false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.aresoft.logic.service.interfaces.BaseLogicService#getDateByPage(int,
	 * int, java.lang.Object[])
	 */
	public Page getDateByPage(int pageNo, int pageSize, Persistent objects) {
		// TODO Auto-generated method stub
//		operating = this.MODULE_OPERATING_SELECT;
		return dao.findForList(defaultSelectSQL, objects, (pageNo - 1) * pageSize,
				pageSize);
	}

	
	public Page getDateByPage(String theSelectSQL,int pageNo, int pageSize, Persistent objects) {
		// TODO Auto-generated method stub
//		operating = this.MODULE_OPERATING_SELECT;
		return dao.findForList(theSelectSQL, objects, (pageNo - 1) * pageSize,
				pageSize);
	}
	
	
	/**
	 * add by shijx 数据 分页查询
	 * @param SQLID
	 * @param pageNo
	 * @param pageSize
	 * @param objects
	 * @return
	 */
	public Page getDateByPage(String SQLID,int pageNo, int pageSize, HashMap map) {
		// TODO Auto-generated method stub
//		operating = this.MODULE_OPERATING_SELECT;
		return dao.findForList(SQLID, map, (pageNo - 1) * pageSize,pageSize);
	}

	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.aresoft.logic.service.interfaces.BaseLogicService#insert(java.lang
	 * .Object)
	 */
	public Object insert(Persistent t) {
		// TODO Auto-generated method stub
//		operating = this.MODULE_OPERATING_INSERT;
		return dao.insert(insertSQL, t);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.aresoft.logic.service.interfaces.BaseLogicService#load(java.lang.Long)
	 */
	public Persistent load(Long id) {
		// TODO Auto-generated method stub
//		operating = this.MODULE_OPERATING_SELECT;
		return (Persistent) dao.load(loadSQL, id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.aresoft.logic.service.interfaces.BaseLogicService#update(java.lang
	 * .Object)
	 */
	public boolean update(Persistent t) {
		// TODO Auto-generated method stub
//		operating = this.MODULE_OPERATING_UPDATE;
		int i = dao.update(updateSQL, t);
		return i > 0 ? true : false;
	}
	
	/**
	 * 无分页数据查询 shijx 2010-8-11
	 * @param objects
	 * @return
	 */
	public List<Persistent> getListData(String sqlName,HashMap map)
	{
//		operating = this.MODULE_OPERATING_SELECT;
		return dao.findForList(sqlName, map);
	}
	
}
